home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 079a.dms / 079a.adf / LHA_ARCHIVES / vpath.lha / VPath.c < prev   
C/C++ Source or Header  |  1992-02-26  |  8KB  |  364 lines

  1. /************************************************************************/
  2. /*    VPath2.c - 6/11/94                        */
  3. /*                                    */
  4. /*    This program reads in a Vista script and generates tween    */
  5. /*    positions based on the key positions already present in the    */
  6. /*    file. Multiple keys supported.                    */
  7. /************************************************************************/
  8.  
  9.  
  10. /* Standard Library Headers */
  11. /* ************************ */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. /* Globals */
  17. /* ******* */
  18. #define EOL '\n'
  19.  
  20. FILE *infile ;
  21. FILE *outfile ;
  22.  
  23. enum SettingHeadings {CamX, CamY, CamZ, Bank, Heading, Pitch} ;
  24. enum WhichSettings   {Start, End, Delta} ;
  25. enum SpareSets         {Base, Running} ;
  26.  
  27. int settings[6][3] ;     /* Coords etc */
  28. float setting_spares[6][2] ;/* Remainders after divides */
  29.  
  30.  
  31. /* Function Prototypes */
  32. /* ******************* */
  33. void process_file(void) ;
  34. void extract_settings(char *string, int keyframe) ;
  35. void calc_deltas(int frame_count) ;
  36. void check_header(char *input_line) ;
  37. void display_frame_info(int key_no, char *data, char *title, char *direction) ;
  38.  
  39. /* Main Program */
  40. /* ************ */
  41. void main(int argc, char *argv[])
  42. {
  43.     /* Check argument count */
  44.     /* ******************** */
  45.     if (argc != 3)
  46.     {
  47.         printf("\n%s Usage: %s Keyframefile Newfile\n\n",
  48.                             argv[0], argv[0]) ;
  49.         exit (0) ;
  50.     }
  51.  
  52.  
  53.     /* Open Files */
  54.     /* ********** */
  55.     infile = fopen(argv[1], "r") ;
  56.     if (infile == NULL)
  57.     {
  58.         printf("\aCannot open %s for input\n\n", argv[1]) ;
  59.         exit(100) ;
  60.     }
  61.  
  62.     outfile = fopen(argv[2], "w") ;
  63.     if (outfile == NULL)
  64.     {
  65.         printf("\aCannot open %s for output\n\n", argv[2]) ;
  66.         fclose(infile) ;
  67.         exit(200) ;
  68.     }
  69.  
  70.  
  71.     printf("\nVPath - Steve Pullinger Nov 94\n"
  72.          "==============================\n\n") ;
  73.     
  74.         
  75.     process_file() ;
  76.  
  77.  
  78.  
  79.     /* All done, close files */
  80.     /* ********************* */
  81.     fclose(outfile) ;
  82.     fclose(infile) ;
  83.     
  84.     /* Say goodbye */
  85.     /* *********** */
  86.     printf("\nOK all done..\n") ;
  87.     printf("Thankyou and goodnight.\n\n") ;
  88.     
  89. }
  90. /* Function Definitions */
  91. /* ******************** */
  92. void process_file(void)
  93. {
  94.     /****************************************************************/
  95.     /*             PROCESS FILE FUNCTION            */
  96.     /*             *********************            */
  97.     /* NAME:    process_file()                    */
  98.     /*                                */
  99.     /* DESCRIPTION: This function creates the tween positions in the*/
  100.     /*        new Vista script.                */
  101.     /*                                */
  102.     /* SYNOPSIS:    stdio.h                     */
  103.     /*        string.h                    */
  104.     /*        void process_file(void)             */
  105.     /*                                */
  106.     /* EXAMPLE:    process_file() ;                */
  107.     /****************************************************************/
  108.  
  109.     char start_line[81] ; /* Read buffer position */
  110.     char end_line[81] ;
  111.     char output_line[81] ; /* Write buffer */
  112.     int count ;
  113.     char key_frame_heading[81] ;
  114.     int num_frames ;
  115.     int difference ;
  116.     int current ;
  117.     int num_keys ;
  118.     int current_key ;
  119.  
  120.     /* Make sure it's a Vista file */
  121.     /* *************************** */
  122.     check_header(key_frame_heading) ;
  123.  
  124.  
  125.     /* Count key positions */
  126.     /* ******************* */
  127.     num_keys = 0 ;
  128.     while (!feof(infile))
  129.     {
  130.         fgets(start_line, 81, infile) ;
  131.         if (!feof(infile))
  132.         {
  133.             if (strchr (start_line, ',') != 0)
  134.                 num_keys++ ;
  135.         }
  136.     }
  137.     if (num_keys > 1)
  138.         printf("\n** File contains %d key frames. **\n", num_keys) ;
  139.     else
  140.     {
  141.         printf("\aScript doesn't have enough key frames\n") ;
  142.         fclose (outfile) ;
  143.         fclose(infile) ;
  144.         exit(100) ;
  145.     }
  146.  
  147.     /* Reset file pointer for re-reading */
  148.     /* ********************************* */
  149.     rewind(infile) ;
  150.     fgets(start_line, 81, infile) ;
  151.     fgets(start_line, 81, infile) ;
  152.  
  153.     current_key = 1 ;
  154.     while (current_key < num_keys)
  155.     {
  156.         /* Read first key position */
  157.         /* *********************** */
  158.         if (current_key == 1)
  159.         {
  160.             fgets(start_line, 81, infile) ;
  161.             fprintf(outfile, "%s", start_line) ; /* Copy key pos
  162.                                 to new file */
  163.  
  164.             
  165.             
  166.             extract_settings(start_line, Start) ; /* Store
  167.                              settings in array */
  168.             
  169.             display_frame_info(current_key,
  170.                 start_line, key_frame_heading, "From") ;
  171.             
  172.             
  173.         }
  174.         else
  175.         {
  176.             for (count = CamX; count <= Pitch; count++)
  177.             {
  178.                 settings[count][Start] = settings[count][End] ;
  179.             
  180.             }
  181.             
  182.             display_frame_info(current_key,
  183.                 end_line, key_frame_heading, "From") ;
  184.         
  185.         }
  186.         
  187.  
  188.         /* Read next key position */
  189.         /* ********************** */
  190.         fgets(end_line, 81, infile) ;
  191.         extract_settings(end_line, End) ;
  192.  
  193.         display_frame_info(current_key + 1, end_line,
  194.                  key_frame_heading, "To") ;
  195.  
  196.         
  197.         /* Request number of frames */
  198.         /* ************************ */
  199.         num_frames = 0 ;
  200.         do
  201.         {
  202.             printf("\nHow many frames from key frame %d"
  203.                 " to key frame %d ? (Minimum 3): ",
  204.                 current_key, current_key + 1) ;
  205.  
  206.             fflush(stdin) ;
  207.  
  208.         } while ((scanf("%d", &num_frames) != 1) || (num_frames < 3)) ;
  209.  
  210.         printf("Computing %d frames...\n", num_frames) ;
  211.         
  212.         /* Calculate deltas */
  213.         /* **************** */
  214.         calc_deltas(num_frames) ;
  215.  
  216.  
  217.         /* Write tween frames to new file */
  218.         /* ****************************** */
  219.         for (count = 2; count < num_frames; count++)
  220.         {
  221.  
  222.             for (current = CamX; current <= Pitch; current++)
  223.             {
  224.                 /* Calculate tween positions */
  225.                 /* ************************* */
  226.                 settings[current][Start] =
  227.                     settings[current][Start] +
  228.                     settings[current][Delta] ;
  229.  
  230.                 setting_spares[current][Running] =
  231.                     setting_spares[current][Running] +
  232.                     setting_spares[current][Base] ;
  233.  
  234.                 if (abs((int)setting_spares[current]
  235.                             [Running]) >= 1)
  236.                 {
  237.                     difference = (int)setting_spares
  238.                                 [current]
  239.                                 [Running] ;
  240.  
  241.                     settings[current][Start] =
  242.                         settings[current][Start] +
  243.                         difference ;
  244.  
  245.                     setting_spares[current][Running] =
  246.                         setting_spares[current]
  247.                         [Running] - difference ;
  248.  
  249.                 }
  250.  
  251.             }
  252.             /* Build outputline */
  253.             /* **************** */
  254.             strcpy(output_line, "") ;
  255.             sprintf(output_line, "%5d, %5d, %5d, %4d, %4d, %4d,\n"
  256.                 ,settings[CamX][Start], settings[CamY][Start],
  257.                 settings[CamZ][Start], settings[Bank][Start],
  258.                 settings[Heading][Start],
  259.                 settings[Pitch][Start]) ;
  260.  
  261.             /* Write line to new file */
  262.             /* ********************** */
  263.             fprintf(outfile, "%s", output_line) ;
  264.  
  265.         }
  266.  
  267.         /* Write last frame */
  268.         /* **************** */
  269.         fprintf(outfile, "%s", end_line) ;
  270.  
  271.         current_key++ ;
  272.     }
  273. }
  274.  
  275.  
  276. void display_frame_info(int key_no, char *data, char *title, char *direction)
  277. {
  278.  
  279.     printf("\n %s Keyframe: %5d\n", direction, key_no) ;
  280.     printf("%s", title) ;
  281.     printf(" =====================================\n") ;
  282.     printf("%s\n", data) ;
  283. }
  284.  
  285. void extract_settings(char *string, int keyframe)
  286. {
  287.     int count ;
  288.     int which_setting ;
  289.     char temp[10] ;
  290.     int temp_count ;
  291.  
  292.     which_setting = CamX ;
  293.     for (count = 0; count < strlen(string); count++)
  294.     {
  295.         /* Store number in temp as a string */
  296.         temp_count = 0 ;
  297.         while ((string[count] != ',') && (string[count] != EOL))
  298.         {
  299.             temp[temp_count] = string[count] ;
  300.             count++ ;
  301.             temp_count++ ;
  302.         }
  303.         temp[temp_count] = '\0' ;
  304.  
  305.         /* Convert temp to number */
  306.         if (which_setting <= Pitch)
  307.         {
  308.             settings[which_setting][keyframe] = atoi(temp) ;
  309.             which_setting++ ;
  310.         }
  311.     }
  312. }
  313.  
  314. void calc_deltas(int frame_count)
  315. {
  316.     int current ;
  317.  
  318.     /* Calculate differences */
  319.     frame_count-- ;
  320.     for (current = CamX; current <= Pitch; current++)
  321.     {
  322.         settings[current][Delta] =
  323.             settings[current][End] - settings[current][Start] ;
  324.     }
  325.  
  326.     /* Calculate Steps */
  327.     for (current = CamX; current <= Pitch; current++)
  328.     {
  329.         setting_spares[current][Base] =
  330.             (float)settings[current][Delta] / (float)frame_count ;
  331.  
  332.         /* Split float to int and decimal remainder */
  333.         settings[current][Delta] =
  334.             (int)setting_spares[current][Base] ;
  335.  
  336.         setting_spares[current][Base] =
  337.             setting_spares[current][Base] -
  338.             settings[current][Delta] ;
  339.     }
  340. }
  341.  
  342. void check_header(char *input_line)
  343. {
  344.  
  345.     int count ;
  346.  
  347.     /* Copy file header */
  348.     for (count = 0; count < 2; count++)
  349.     {
  350.         fgets(input_line, 81, infile) ;
  351.         if (count == 0)
  352.         {
  353.             if (strcmp(input_line, "Vista Script File\n") != 0)
  354.             {
  355.                 printf("\aERROR: Not a Vista File1\n") ;
  356.                 fclose(outfile) ;
  357.                 fclose(infile) ;
  358.                 exit (100) ;
  359.             }
  360.         }
  361.         fprintf(outfile, "%s", input_line) ;
  362.     }
  363. }
  364.